/* GUI CONVERTED from soiv1011.p (converter v1.75) Thu Jul 19 05:36:02 2001 */
/* soiv1011.p - SALES ORDER INVOICE RE PRINT NOTA FISCAL FORMAT              */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                       */
/* All rights reserved worldwide.  This is an unpublished work.              */
/* $Revision: 1.8.1.5 $                                                  */
/*V8:ConvertMode=Report                                                      */
/* REVISION: 8.6      CREATED:       10/20/97   BY: *H1F8* Nirav Parikh      */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane         */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan        */
/* REVISION: 8.6E     LAST MODIFIED: 06/24/98   BY: *L024* Sami Kureishy     */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane  */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KN* myb               */
/* REVISION: 9.1      LAST MODIFIED: 08/30/00   BY: *N0Q8* Arul Victoria     */
/* Revision: 1.8.1.4     BY: Katie Hilbert  DATE: 04/01/01 ECO: *P002* */
/* $Revision: 1.8.1.5 $   BY: Ellen Borden   DATE: 07/09/01  ECO: *P007* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* THIS PROGRAM IS SIMILAR TO sorp1001.p. CHANGES DONE IN THIS PROGRAM     */
/* MAY ALSO NEED TO BE DONE IN sorp1001.p.                                 */


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE soiv1011_p_1 "Override Print Inv Hist"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define new shared variable rndmthd           like rnd_rnd_mthd.
define new shared variable ih_recno          as recid.
define new shared variable comb_inv_nbr      like ih_inv_nbr.
define new shared variable max_lines         as integer.
define new shared variable body_count        as integer.
define new shared variable undo_nota         like mfc_logical.
define     shared variable override_print_hist  like mfc_logical
   label {&soiv1011_p_1}.
define     shared variable inv               like ih_inv_nbr.
define     shared variable inv1              like ih_inv_nbr.
define     shared variable nbr               like ih_nbr.
define     shared variable nbr1              like ih_nbr.
define     shared variable shipdate          like ih_ship_date.
define     shared variable shipdate1         like shipdate.
define     shared variable inv_date          like ih_inv_date.
define     shared variable inv_date1         like ih_inv_date.
define     shared variable cust              like ih_cust.
define     shared variable cust1             like ih_cust.
define     shared variable bill              like ih_bill.
define     shared variable bill1             like ih_bill.
define     shared variable site              like ih_site.
define     shared variable site1             like ih_site.
define     shared variable company           as character
   format "x(38)" extent 6.
define            variable item_count        as integer no-undo.
define            variable addr              as character
   format "x(38)" extent 6 no-undo.
define            variable soldto            as character
   format "x(38)" extent 6 no-undo.
define            variable comp_addr         like soc_company no-undo.
define            variable numero            like ih_inv_nbr
   extent 24 no-undo.
define            variable valor             like idh_price
   extent 24 no-undo.
define            variable data              like ih_due_date
   extent 24 no-undo.
define            variable pct_due           like ctd_pct_due
   extent 24 no-undo.
define            variable bal_due           like idh_price no-undo.
define            variable numero1           like ih_inv_nbr no-undo.
define            variable numero2           like ih_inv_nbr no-undo.
define            variable valor1            like idh_price no-undo.
define            variable valor2            like idh_price no-undo.
define            variable data1             like ih_due_date no-undo.
define            variable data2             like ih_due_date no-undo.
define            variable trl_length        as integer
   initial 11 no-undo.
define            variable shipto            as character
   format "x(38)" extent 6 no-undo.
define            variable prepaid-lbl       as character
   format "x(12)" no-undo.
define            variable po-lbl            as character
   format "x(8)" no-undo.
define            variable lot-lbl           as character
   format "x(43)" no-undo.
define            variable cspart-lbl        as character
   format "x(15)" no-undo.
define            variable hdr_po            as character
   format "x(38)" no-undo.
define            variable po-lbl2           as character
   format "x(16)" no-undo.
define            variable letter            as character
   format "x(1)" extent 24 no-undo.
define            variable lot_total         like tr_qty_chg no-undo.
define            variable sales_entity      like si_entity no-undo.
define            variable due_date          like ih_due_date no-undo.
define            variable disc_date         like ih_due_date no-undo.
define            variable qty_inv           like idh_qty_inv no-undo.
define            variable ext_price         like idh_price no-undo.
define            variable desc1             like idh_desc no-undo.
define            variable desc2             like idh_desc no-undo.
define            variable termsdesc         like ct_desc no-undo.
define            variable tot_merca         like ih_trl1_amt no-undo.
define            variable tot_nota          like ih_trl1_amt no-undo.
define            variable tot_trans         like ih_trl1_amt no-undo.
define            variable tot_seguro        like ih_trl1_amt no-undo.
define            variable tot_outras        like ih_trl1_amt no-undo.
define            variable base_ipi          like ih_trl1_amt no-undo.
define            variable valor_ipi         like ih_trl1_amt no-undo.
define            variable base_icms         like ih_trl1_amt no-undo.
define            variable valor_icms        like ih_trl1_amt no-undo.
define            variable aliq_icms         like ih_disc_pct no-undo.
define            variable icms_rate         like ih_disc_pct no-undo.
define            variable fiscal_class      like pt_fiscal_class no-undo.
define            variable ipi_rate          like tx2_tax_pct no-undo.
define            variable ipi_tax_amt       like ih_trl1_amt no-undo.
define            variable comp_cgc_id       like ad_gst_id no-undo.
define            variable comp_est_id       like ad_pst_id no-undo.
define            variable comp_state        like ad_state no-undo.
define            variable sold_cgc_id       like ad_gst_id no-undo.
define            variable sold_est_id       like ad_pst_id no-undo.
define            variable sold_state        like ad_state no-undo.
define            variable ship_cgc_id       like ad_gst_id no-undo.
define            variable ship_est_id       like ad_pst_id no-undo.
define            variable ship_state        like ad_state no-undo.
define            variable disc_pct          like ctd_pct_due no-undo.
define            variable no_pi             like ih_po no-undo.
define            variable vend              like ih_source no-undo.
define            variable cfo               like ih_tax_usage no-undo.
define            variable natureza          like ad_name no-undo.
define            variable ped_client        like ih_po no-undo.
define            variable carr_name         like ad_name no-undo.
define            variable via               like fr_mode no-undo.
define            variable frete             like ih_fr_terms no-undo.
define            variable carr_ender        like ad_city no-undo.
define            variable ship_wt           like idh_fr_wt no-undo.
define            variable net_wt            like idh_fr_wt no-undo.
define            variable contra            like ih_fob no-undo.
define            variable tot_disc_amt      like ih_disc_pct no-undo.
define            variable shipper_id        like ih_inv_nbr no-undo.
define            variable abs_shipvia       like ih_shipvia no-undo.
define            variable abs_fob           like ih_fob no-undo.
define            variable abs_trans_mode    as character
   format "x(20)" no-undo.
define            variable abs_veh_ref       as character
   format "x(20)" no-undo.
define            variable oldsession        as character no-undo.
define            variable oldcurr           like ih_curr no-undo.
/* DEFINE VARIABLES FOR FORMATS */
define            variable valor_fmt         as character no-undo.
define            variable valor_old         as character no-undo.
define            variable ext_price_fmt     as character no-undo.
define            variable ext_price_old     as character no-undo.
define            variable ipi_tax_fmt       as character no-undo.
define            variable ipi_tax_old       as character no-undo.
define            variable tot_trans_fmt     as character no-undo.
define            variable tot_trans_old     as character no-undo.
define            variable tot_seguro_fmt    as character no-undo.
define            variable tot_seguro_old    as character no-undo.
define            variable tot_outras_fmt    as character no-undo.
define            variable tot_outras_old    as character no-undo.
define            variable tot_merca_fmt     as character no-undo.
define            variable tot_merca_old     as character no-undo.
define            variable tot_nota_fmt      as character no-undo.
define            variable tot_nota_old      as character no-undo.
define            variable base_ipi_fmt      as character no-undo.
define            variable base_ipi_old      as character no-undo.
define            variable valor_ipi_fmt     as character no-undo.
define            variable valor_ipi_old     as character no-undo.
define            variable base_icms_fmt     as character no-undo.
define            variable base_icms_old     as character no-undo.
define            variable valor_icms_fmt    as character no-undo.
define            variable valor_icms_old    as character no-undo.
define            variable mc-error-number   as integer   no-undo.

{gpvtecdf.i &var="shared"}
/* Define vars for VAT Reg No & Country */
{gpvtepdf.i &var=" "}
{mfsotrla.i "NEW"}

/* ASSIGN ORIGINAL FORMAT TO _OLD VARIABLES */
assign
   nontax_old   = nontaxable_amt:format
   taxable_old  = taxable_amt:format
   line_tot_old = line_total:format
   trl_amt_old  = so_trl1_amt:format
   tax_amt_old  = tax_amt:format
   container_old = container_charge_total:format
   line_charge_old = line_charge_total:format
   ord_amt_old  = ord_amt:format.

/* FRAME a IS FOR HEADER COMMENTS */
FORM /*GUI*/ 
   item_count     at 1   format ">9"
   idh_um         at 4   format "x(2)"
   qty_inv        at 6   format ">>>,>>9"
   idh_fr_wt      at 15  format ">>>,>>9"
   idh_part       at 23  format "x(10)"
   desc1          at 34  format "x(24)"
   fiscal_class   at 60  format "x(10)"
   natureza       at 71  format "x(3)"
   idh_price      at 75  format ">>>>,>>>,>>>.99"
   ext_price      at 91  format ">>>>>,>>>,>>>.99"
   ipi_rate       at 107 format ">>>9.9"
   ipi_tax_amt    at 113 format ">>>,>>>,>>>.99"
with STREAM-IO /*GUI*/  frame b down no-attr-space no-box no-labels width 132.

FORM /*GUI*/ 
   icms_rate      at 30  format ">9.9"
   tot_trans      at 41  format "->>>>>>>>>>>.99"
   tot_seguro     at 57  format "->>>>>>>>>>>.99"
   tot_outras     at 74  format "->>>>>>>>>>>.99"
   tot_merca      at 92  format "->>>>>>>>>>>.99"
   tot_nota       at 111 format "->>>>>>>>>>>.99" skip (1)
with STREAM-IO /*GUI*/  frame c no-attr-space no-box no-labels width 132.

FORM /*GUI*/ 
   inv_date       at 24
   comp_cgc_id    at 42  format "x(18)"
   ship_cgc_id    at 75  format "x(18)"
   comp_est_id    at 42  format "x(18)"
   comp_state     at 63  format "x(4)"
   ship_est_id    at 75  format "x(18)"
   ship_state     at 95  format "x(4)"
   ih_ship_date   at 101 skip (1)
   base_ipi       at 15  format "->>>>>>>>>>>.99"
   valor_ipi      at 37  format "->>>>>>>>>>>.99"
   base_icms      at 59  format "->>>>>>>>>>>.99"
   valor_icms     at 81  format "->>>>>>>>>>>.99"
   aliq_icms      at 98  format ">9.9" skip (1)
with STREAM-IO /*GUI*/  frame d no-attr-space no-box no-labels width 132.

FORM /*GUI*/ 
   no_pi          at 81  format "x(22)"
   vend           at 110 format "x(8)"
   cfo            at 50  format "x(6)"
   natureza       at 70  format "x(30)"
   ped_client     at 105 format "x(28)"
   carr_name      at 60  format "x(28)"
   via            at 90  format "x(8)"
   frete          at 110 format "x(8)"
   ih_inv_nbr     at 30
   carr_ender     at 60  format "x(28)"
   contra         at 110 format "x(20)"
   abs_trans_mode at 50
   abs_veh_ref    at 75
   ship_wt        at 95
   ih_ship_date   at 32
   net_wt         at 95 skip (1)
with STREAM-IO /*GUI*/  frame e no-attr-space no-box no-labels width 132.

FORM /*GUI*/ 
   numero1        at 1   format "x(9)"
   valor1         at 11  format ">>>>>,>>>,>>>.99"
   data1          at 28
   numero2        at 38  format "x(9)"
   valor2         at 48  format ">>>>>,>>>,>>>.99"
   data2          at 65
   addr[1]        at 85
with STREAM-IO /*GUI*/  frame f down no-attr-space no-box no-labels width 132.

FORM /*GUI*/ 
   skip (1)
   sold_cgc_id    at 85  format "x(18)"
   sold_est_id    at 113 format "x(18)"
   disc_pct       at 94  format "->9.9"
   disc_date      at 110
   data[1]        at 125
   termsdesc      at 96
   shipto[5]      at 94
   ship_cgc_id    at 85  format "x(18)"
   ship_est_id    at 113 format "x(18)"
with STREAM-IO /*GUI*/  frame g no-attr-space no-box no-labels width 132.

/* SET _OLD VARIABLES WITH FORMATS */
assign
   valor_old      = valor1:format
   ext_price_old  = ext_price:format
   ipi_tax_old    = ipi_tax_amt:format
   tot_trans_old  = tot_trans:format
   tot_seguro_old = tot_seguro:format
   tot_merca_old  = tot_seguro:format
   tot_outras_old = tot_outras:format
   tot_nota_old   = tot_nota:format
   base_ipi_old   = base_ipi:format
   valor_ipi_old  = valor_ipi:format
   base_icms_old  = base_icms:format
   valor_icms_old = valor_icms:format.

find first rmc_ctrl no-lock.
/* VERIFY THAT Nota Fiscal DOESN'T EXCEED max_lines */
{gprun.i ""soivcksz.p""}
if max_lines < 0 then do:
   max_lines = max_lines * -1.
   undo_nota = yes.
   leave.
end. /* END OF max_lines < 0 */

/* DUPLICATA LETTER SUFFIX */
do i = 1 to 24:
   letter[i] = chr((ascending("a") - 1) + i).
end. /* END OF DO i = 1 TO 24 */

oldsession = SESSION:numeric-format.

for each ih_hist where
      (override_print_hist or ih_inv_mthd = ""
      or ih_inv_mthd begins " "
      or ih_inv_mthd begins "b"
      or ih_inv_mthd begins "p")
      and (ih_inv_nbr >= inv and ih_inv_nbr <= inv1)
      and (ih_nbr >= nbr and ih_nbr <= nbr1)
      and (ih_ship_date >= shipdate and ih_ship_date <= shipdate1)
      and (ih_inv_date >= inv_date and ih_inv_date <= inv_date1)
      and (ih_cust >= cust and ih_cust <= cust1)
      and (ih_bill >= bill and ih_bill <= bill1)
      and (ih_site >= site and ih_site <= site1)
    AND LOOKUP(ih_site,GLOBAL_user_site) > 0 
      no-lock break by ih_inv_nbr by ih_nbr:

   if (oldcurr <> ih_curr) or (oldcurr = "") then do:

      /* GET ROUNDING METHOD FROM CURRENCY MASTER */
      {gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
         "(input  ih_curr,
           output rndmthd,
           output mc-error-number)"}
      if mc-error-number <> 0 then do:
         {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=4}
         next.
      end.

      find rnd_mstr where rnd_rnd_mthd = rndmthd no-lock no-error.
      if not available rnd_mstr then do:
         /* ROUND METHOD RECORD NOT FOUND */
         {pxmsg.i &MSGNUM=863 &ERRORLEVEL=4}
         next.
      end. /* END OF IF AVAIL rnd_mstr */

      /* IF RND_DEC_PT = COMMA FOR THE DECIMAL POINT */
      /* THIS IS THE EUROPEAN STYLE CURRENCY */

      if (rnd_dec_pt = ",")
      then
         SESSION:numeric-format = "European".
      else
         SESSION:numeric-format = "American".
      /* FORMAT ALL AMOUNT VARIABLES */
      valor_fmt = valor_old.

      {gprun.i ""gpcurfmt.p"" "(input-output valor_fmt,
                                        input rndmthd)"}
      assign
         valor1:format = valor_fmt
         valor2:format = valor_fmt
         ext_price_fmt = ext_price_old.

      {gprun.i ""gpcurfmt.p"" "(input-output ext_price_fmt,
                                        input rndmthd)"}
      assign
         ext_price:format = ext_price_fmt
         ipi_tax_fmt      = ipi_tax_old.

      {gprun.i ""gpcurfmt.p"" "(input-output ipi_tax_fmt,
                                        input rndmthd)"}
      assign
         ipi_tax_amt:format = ipi_tax_fmt
         tot_trans_fmt      = tot_trans_old.

      {gprun.i ""gpcurfmt.p"" "(input-output tot_trans_fmt,
                                        input rndmthd)"}
      assign
         tot_trans:format = tot_trans_fmt
         tot_seguro_fmt   = tot_seguro_old.

      {gprun.i ""gpcurfmt.p"" "(input-output tot_seguro_fmt,
                                        input rndmthd)"}
      assign
         tot_seguro:format = tot_seguro_fmt
         tot_outras_fmt    = tot_outras_old.

      {gprun.i ""gpcurfmt.p"" "(input-output tot_outras_fmt,
                                        input rndmthd)"}
      assign
         tot_outras:format = tot_outras_fmt
         tot_merca_fmt     = tot_merca_old.

      {gprun.i ""gpcurfmt.p"" "(input-output tot_merca_fmt,
                                        input rndmthd)"}
      assign
         tot_merca:format = tot_merca_fmt
         tot_nota_fmt     = tot_nota_old.

      {gprun.i ""gpcurfmt.p"" "(input-output tot_nota_fmt,
                                        input rndmthd)"}
      assign
         tot_nota:format = tot_nota_fmt
         base_ipi_fmt    = base_ipi_old.

      {gprun.i ""gpcurfmt.p"" "(input-output base_ipi_fmt,
                                        input rndmthd)"}
      assign
         base_ipi:format = base_ipi_fmt
         valor_ipi_fmt   = valor_ipi_old.

      {gprun.i ""gpcurfmt.p"" "(input-output valor_ipi_fmt,
                                        input rndmthd)"}
      assign
         valor_ipi:format = valor_ipi_fmt
         base_icms_fmt    = base_icms_old.

      {gprun.i ""gpcurfmt.p"" "(input-output base_icms_fmt,
                                        input rndmthd)"}
      assign
         base_icms:format = base_icms_fmt
         valor_icms_fmt   = valor_icms_old.

      {gprun.i ""gpcurfmt.p"" "(input-output valor_icms_fmt,
                                        input rndmthd)"}
      assign
         valor_icms:format = valor_icms_fmt
         oldcurr    = ih_curr.
   end.  /* IF (OLDCURR <> ih_CURR) */

   if first-of(ih_inv_nbr) then do:
      comb_inv_nbr = ih_inv_nbr.

      /* PRODUCING ESTABLISHMENT - ESTABELECIMENTO ENITENTE      */
      /* STATE, GST & PST ID     - U.F., INSCR. C.G.C & ESTADUAL */
      find ad_mstr where ad_addr = ih_site and ad_type = "company"
         no-lock no-error.
      if available ad_mstr then
      assign
         comp_state = ad_state
         comp_cgc_id = ad_gst_id
         comp_est_id = ad_pst_id.
      else
      assign
         comp_state = ""
         comp_cgc_id = ""
         comp_est_id = "".

      /* SOLD TO */
      find ad_mstr where ad_addr = ih_cust no-lock no-error.
      update soldto = "".
      if available ad_mstr then do:
         assign
            addr[1] = ad_name
            addr[2] = ad_line1
            addr[3] = ad_line2
            addr[4] = ad_line3
            addr[6] = ad_country
            sold_state = ad_state
            sold_cgc_id = ad_gst_id
            sold_est_id = ad_pst_id.
         {mfcsz.i addr[5] ad_city ad_state ad_zip}.
         {gprun.i ""gpaddr.p"" }
         assign
            soldto[1] = addr[1]
            soldto[2] = addr[2]
            soldto[3] = addr[3]
            soldto[4] = addr[4]
            soldto[5] = addr[5]
            soldto[6] = addr[6].
      end. /* END OF IF AVAIL ad_mstr */

      /* SHIP TO */
      find ad_mstr where ad_addr = ih_ship no-lock no-error.
      update shipto = "".
      if available ad_mstr then do:
         assign
            addr[1] = ad_name
            addr[2] = ad_line1
            addr[3] = ad_line2
            addr[4] = ad_line3
            addr[6] = ad_country
            ship_state = ad_state
            ship_cgc_id = ad_gst_id
            ship_est_id = ad_pst_id.
         {mfcsz.i addr[5] ad_city ad_state ad_zip}.
         {gprun.i ""gpaddr.p"" }
         assign
            shipto[1] = addr[1]
            shipto[2] = addr[2]
            shipto[3] = addr[3]
            shipto[4] = addr[4]
            shipto[5] = addr[5]
            shipto[6] = addr[6].

         /* FIND VAT # FOR SHIP TO OR BILL TO IF SHIP TO VAT=" " */

         if ad_pst_id = "" then do:
            find ad_mstr where ad_addr = ih_bill no-lock no-error.
            if available ad_mstr then do:
               {gpvteprg.i}
            end. /* END OF IF AVAIL ad_mster */
         end. /* END OF IF ad_pst_id = "" */
         else do:
            {gpvteprg.i}
         end. /* END OF IF ad_pst_id <> "" */
      end.  /* END Of IF AVAILABLE ad_mstr */

      /* INITIALIZE DUPLICATA ARRAY */
      assign
         numero     = ""
         data       = ?
         pct_due    = 0
         disc_pct   = 0
         disc_date  = ?
         i          = 1
         numero[i]  = ih_inv_nbr + letter[i]
         inv_date   = ih_inv_date
         data[i]    = inv_date
         pct_due[i] = 100
         termsdesc  = "".
      find ct_mstr where ct_code = ih_cr_terms no-lock no-error.
      if available ct_mstr then do:
         assign
            termsdesc = ct_desc
            disc_pct  = ct_disc_pct
            /* DUE & DISCOUNT DATE */
            disc_date = ?
            due_date  = ?.
         if ct_dating = no then do:
            /* SINGLE PAYMENT CREDIT TERMS */
            {gprun.i ""adctrms.p"" "(input inv_date,
                                     input ct_code,
                                     output disc_date,
                                     output due_date)"}
            data[i] = due_date.
         end. /* END OF IF ct_dating = no */
         else do:
            /* MULTI PAYMENT CREDIT TERMS */
            if ct_dating = yes then do:
               for each ctd_det where ctd_code = ih_cr_terms
                     no-lock use-index ctd_cdseq:
                  find ct_mstr where ct_code = ctd_date_cd
                     no-lock no-error.
                  if available ct_mstr then do:
                     {gprun.i ""adctrms.p"" "(input inv_date,
                                              input ct_code,
                                              output disc_date,
                                              output due_date)"}
                     assign
                        numero[i] = ih_inv_nbr + letter[i]
                        data[i] = due_date
                        pct_due[i] = ctd_pct_due.
                  end. /* END OF IF AVAIL ct_mstr */

                  i = i + 1.
                  if i > 24 then leave.
               end. /* END OF FOR EACH ctd_det */

            end. /* END OF IF ct_dating = yes */

         end. /* END OF IF ct_dating <> no */

      end. /* END OF IF AVAIL ct_mstr */

      if disc_pct = 0 then disc_date = ?.

      /* SHIPPER MASTER */
      find abs_mstr where abs_shipfrom = ih_site and
         abs_id = "s" + comb_inv_nbr no-lock no-error.

      if available abs_mstr then
      assign
         abs_shipvia = substring(abs__qad01,1,20)
         abs_fob = substring(abs__qad01,21,20)
         abs_trans_mode = substring(abs__qad01,61,20)
         abs_veh_ref = substring(abs__qad01,81,20).

      /* INITIALIZE TOTALS */
      assign
         tot_disc_amt = 0
         tot_merca    = 0
         tot_nota     = 0
         valor_ipi    = 0
         valor_icms   = 0
         net_wt       = 0
         item_count   = 0
         body_count   = 0
         icms_rate    = 0
         aliq_icms    = 0
         natureza     = ""
         no_pi        = ""
         vend         = ih_slspsn[1]
         ped_client   = ih_po
         cfo          = ih_tax_usage
         ship_wt      = ih_weight
         frete        = ih_fr_terms
         contra       = ih_fob.

      put "" skip(6).

   end.  /* END OF IF FIRST-OF(ih_inv_nbr) */

   /* UPDATE ih_hist AS INVOICED */
   ih_recno = recid(ih_hist).

   /* PRINT HEADER COMMENTS BEFORE LINE ITEMS  - OBSERV */
   for each cmt_det no-lock where cmt_indx = ih_cmtindx
         and (lookup ("BL",cmt_print) > 0 or
         lookup ("PA",cmt_print) > 0 or
         lookup ("SH",cmt_print) > 0 or
         lookup ("IN",cmt_print) > 0):

      do i = 1 to 15:

         if cmt_cmmt[i] <> "" then do:
            put cmt_cmmt[i] at 3 skip.
            body_count = body_count + 1.
         end. /* IF cmt_cmmt[i] <> "" */

      end. /* END OF DO i = 1 TO 15 */

   end. /* END OF FOR EACH cmt_det */

   shipper_id = string(ih_site,"x(8)") + "s" + comb_inv_nbr.

   /* LINE ITEM LOOP */
   for each idh_hist where idh_inv_nbr = ih_inv_nbr
         and idh_qty_inv <> 0
         by idh_line with frame b:
      if idh_qty_inv < 0 then
         qty_inv = idh_qty_inv * -1.
      else
         qty_inv = idh_qty_inv.
      ext_price = qty_inv * idh_price.

      /* GET ITEM MASTER DATA */
      find pt_mstr where pt_part = idh_part no-lock no-error.
      if available pt_mstr then do:
         fiscal_class = pt_fiscal_class.
         if idh_desc = "" then
            desc1 = pt_desc1.
         else
            desc1 = idh_desc.
         desc2 = pt_desc2.
      end. /* END OF IF AVAIL pt_mstr */
      else
      assign
         fiscal_class = ""
         desc1 = idh_desc
         desc2 = "".

      /* LINE SALES TAX */
      ipi_tax_amt = 0.
      for each tx2d_det where tx2d_ref = ih_inv_nbr
            and tx2d_nbr = ih_nbr
            and tx2d_line = idh_line
            and tx2d_tr_type = "16"
            no-lock.
         find tx2_mstr where tx2_tax_code = tx2d_tax_code
            and tx2_tax_code <> "00000000"
            no-lock no-error.
         if available tx2_mstr then do:
            natureza = tx2_tax_usage.
            if tx2d_tax_in then
               icms_rate = round(tx2_tax_pct,1).
            else
               ipi_tax_amt = ipi_tax_amt + tx2d_cur_tax_amt.
         end.  /* END OF IF AVAIL tx2_mstr */
      end. /* END OF FOR EACH tx2d_det */

      if ipi_tax_amt < 0 then ipi_tax_amt = - ipi_tax_amt.

      if ext_price = 0 then ipi_rate = 0.
      else ipi_rate = round(((ipi_tax_amt / ext_price) * 100),1).
      aliq_icms = icms_rate.

      item_count = item_count + 1.

      /* PRINT LINE ITEM */
      display
         item_count      no-label
         idh_um          no-label
         qty_inv         no-label
         idh_fr_wt       no-label
         idh_part        no-label
         desc1           no-label
         fiscal_class    no-label
         natureza        no-label
         idh_price       no-label
         ext_price       no-label
         ipi_rate        no-label when (ipi_rate <> 0)
         ipi_tax_amt     no-label when (ipi_tax_amt <> 0) WITH STREAM-IO /*GUI*/ .
      down 1.
      body_count = body_count + 1.

      if desc2 <> "" then do:
         put desc2 at 36 skip.
         body_count = body_count + 1.
      end. /* END OF IF desc2 <> "" */

      if idh_custpart <> "" then do:
         /* CUSTOMER PART */
         put "CLIENTE CODIGO PRODUTO:" at 10 idh_custpart at 36 skip.
         body_count = body_count + 1.
      end. /* END OF IF idh_custpart <> "" */

      if idh_contr_id <> "" then do:
         /* LINE PURCHASE ORDER*/
         put "     Pedido do Cliente:" at 10 idh_contr_id at 36 skip.
         body_count = body_count + 1.
      end. /* END OF IF idh_contr_id <> "" */

      /* PRINT LINE COMMENTS AFTER OTHER DATA FOR THE LINE */
      for each cmt_det no-lock where cmt_indx = idh_cmtindx
            and (lookup ("BL",cmt_print) > 0 or
            lookup ("PA",cmt_print) > 0 or
            lookup ("SH",cmt_print) > 0 or
            lookup ("IN",cmt_print) > 0):

         do i = 1 to 15:

            if cmt_cmmt[i] <> "" then do:
               put cmt_cmmt[i] at 3 skip.
               body_count = body_count + 1.
            end. /* cmt_cmmt[i] <> "" */

         end. /* END OF DO i =1 TO 15 */

      end. /* END OF FOR EACH cmt_det */

      /* TOTAL LINES  -  TOTAL MERCADORIAS */
      assign
         tot_merca = tot_merca + ext_price
         valor_ipi = valor_ipi + ipi_tax_amt
         net_wt = net_wt + (idh_fr_wt * qty_inv).

      
/*GUI*/ {mfguichk.i } /*Replace mfrpchk*/

   end.  /* END OF FOR EACH idh_hist */

   /* GET TRAILER TAX DATA */
   /* Not Req'd */
   assign
      tot_trans  = tot_trans  + ih_trl1_amt
      tot_seguro = tot_seguro + ih_trl2_amt
      tot_outras = tot_outras + ih_trl3_amt.

   if last-of(ih_inv_nbr) then do:

      /* CONSOLIDATE INVOICE TOTALS */
      assign
         tot_nota = tot_merca + valor_ipi + tot_trans + tot_seguro + tot_outras
         base_ipi = tot_merca
         base_icms = tot_nota
         valor_icms = base_icms * aliq_icms / 100
         /* CALCULATE DUE AMTS FOR MULTIPLE DUE DATES */
         bal_due = tot_nota
         i = 1.

      do while i < 25:

         if pct_due[i] <> 0 then do:
            valor[i] = round((tot_nota * pct_due[i] / 100),2).

            if valor[i] > bal_due then valor[i] = bal_due.
            bal_due = bal_due - valor[i].
         end. /* END OF IF pct_due[i] <> 0 */

         i = i + 1.
      end. /* END OF DO WHILE i < 25 */

      do while body_count < (max_lines + 1):
         body_count = body_count + 1.
         put "" skip.
      end. /* END OF DO WHILE body_count < max_lines + 1 */

      /* PRINT TOTALS */
      display
         icms_rate
         tot_trans
         tot_seguro
         tot_outras
         tot_merca
         tot_nota
      with frame c no-labels STREAM-IO /*GUI*/ .

      /* PRINT SHIP DATE BLOCK - DATA DE EMISSAO */
      display
         inv_date
         comp_cgc_id
         ship_cgc_id
         comp_est_id
         comp_state
         ship_est_id
         ship_state
         ih_ship_date
         base_ipi
         valor_ipi
         base_icms
         valor_icms
         aliq_icms
      with frame d no-labels STREAM-IO /*GUI*/ .

      /* FIND CARRIER - TRANSP */
      carr_name = "".
      carr_ender = "".
      find vd_mstr where vd_addr = substring(ih_shipvia,1,8)
         no-lock no-error.
      if available vd_mstr then do:
         find ad_mstr where ad_addr = vd_addr
            no-lock no-error.

         if available ad_mstr then
         assign
            carr_name = ad_name
            carr_ender = ad_city + " " + ad_state.

      end. /* END OF IF AVAIL vd_mstr */

      find fr_mstr where fr_list = ih_fr_list
         and fr_site = ih_site and fr_curr = ih_curr
         no-lock no-error.
      if available fr_mstr then via = fr_mode.
      else via = "".

      /* PRINT INVOICE NUMBER BLOCK */
      display
         no_pi
         vend
         cfo
         natureza
         ped_client
         carr_name
         via
         frete
         ih_inv_nbr
         carr_ender
         contra
         abs_trans_mode
         abs_veh_ref
         ship_wt
         ih_ship_date
         net_wt
      with frame e no-labels STREAM-IO /*GUI*/ .

      /* PRINT DUPLICATAS & ADDR */

      do i = 1 to 6 with frame f:
         display
            numero[i] @ numero1 no-label
            valor[i] when (valor[i] <> 0) @ valor1 no-label
            data[i] @ data1 no-label
            numero[i + 12] @ numero2 no-label
            valor[i + 12] when (valor[i + 12] <> 0) @ valor2 no-label
            data[i + 12] @ data2 no-label
            soldto[i] @ addr[1] no-label WITH STREAM-IO /*GUI*/ .
         down 1.
      end. /* END OF  DO i = 1 TO 6 */

      do i = 7 to 8 with frame f:
         display
            numero[i] @ numero1 no-label
            valor[i] when (valor[i] <> 0) @ valor1 no-label
            data[i] @ data1 no-label
            numero[i + 12] @ numero2 no-label
            valor[i + 12] when (valor[i + 12] <> 0) @ valor2 no-label
            data[i + 12] @ data2 no-label WITH STREAM-IO /*GUI*/ .
         down 1.
      end. /* END OF DO i = 7 TO 8 */

      do i = 9 to 12 with frame f:
         display
            numero[i] @ numero1 no-label
            valor[i] when (valor[i] <> 0) @ valor1 no-label
            data[i] @ data1 no-label
            numero[i + 12] @ numero2 no-label
            valor[i + 12] when (valor[i + 12] <> 0) @ valor2 no-label
            data[i + 12] @ data2 no-label
            shipto[i - 8] @ addr[1] no-label WITH STREAM-IO /*GUI*/ .
         down 1.
      end. /* END OF DO i = 9 TO 12 */

      do i = 5 to 6 with frame f:
         display
            shipto[i] @ addr[1] no-label WITH STREAM-IO /*GUI*/ .
         down 1.
      end. /* END OF  DO i = 5 TO 6 */

      /* PRINT CREDIT TERMS */
      display
         sold_cgc_id
         sold_est_id
         disc_pct when (disc_pct <> 0)
         disc_date
         data[1]
         termsdesc
         shipto[5]
         ship_cgc_id
         ship_est_id
      with frame g no-labels STREAM-IO /*GUI*/ .
   end.  /* END OF IF LAST-OF(ih_inv_nbr) */

end.  /* END OF FOR EACH IH_HIST */
SESSION:numeric-format = oldsession.
